﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChGK.Data;

namespace CHGK.web.UI.Models
{
    public class TourRepositry
    {
        CHGKModelContainer _CHGKContainer;

        public TourRepositry()
        {
            _CHGKContainer = new CHGKModelContainer();
        }
        
        public TourRepositry(CHGKModelContainer CHGKContainer)
        {
            _CHGKContainer = CHGKContainer;
        }

        public IEnumerable<Tour> GetTours(int compId)
        {
            Competition dbCompetition = _CHGKContainer.Competitions.SingleOrDefault(t => t.ID == compId);
           
            if (dbCompetition == null)
            {
                return null;
            }
            else
            {
                IEnumerable<Tour> _Tours = _CHGKContainer.Tours.Where (t => t.CompetitionID == compId).OrderBy(t=>t.NumberInCompetition);
                
                if (_Tours == null)
                {
                    return null;
                }
                else
                {
                    return _Tours;
                }
            }
        }

        /// <summary>
        /// Возвращает тур с передаваемым ID.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Tour GetTour(int id)
        {
            Tour TourTemp = _CHGKContainer.Tours.SingleOrDefault(t => t.ID == id);
           
            if (TourTemp == null)
            {
                return null;
            }
            else
            {
                return TourTemp;
            }
        }

        public Tour GetTour(int CompId, int Number)
        {
            Tour _Tour = _CHGKContainer.Tours.SingleOrDefault(t => t.CompetitionID == CompId && t.NumberInCompetition == Number);
            
            if (_Tour == null)
            {
                return null;
            }
            else
            {
                return _Tour;
            }
        }

        public Tour GetFirstTour(int CompId)
        {
            Tour _Tour = _CHGKContainer.Tours.FirstOrDefault(t => t.CompetitionID == CompId);
           
            if (_Tour == null)
            {
                return null;
            }
            else
            {
                return _Tour;
            }
        }

        public Tour GetLastTour(int CompId)
        {
            IEnumerable<Tour> _Tours = GetTours(CompId);

            if (_Tours == null)
            {
                return null;
            }
            else
            {
                return _Tours.LastOrDefault();
            }
        }

        public Tour CreateTour(int compId, short numberQuest, short NumberInComp)
        {
            Competition dbCompetition = _CHGKContainer.Competitions.SingleOrDefault(t => t.ID == compId);
            Tour NewTour = new Tour();
           
            if (dbCompetition == null || NewTour == null)
            {
                return null;
            }
            else
            {
                NewTour.NumberOfQuestions = numberQuest;
                NewTour.CompetitionID = compId;
                NewTour.Name = null;
                NewTour.NumberInCompetition = NumberInComp;
              
                _CHGKContainer.Tours.AddObject(NewTour);
                _CHGKContainer.SaveChanges();
                
                return NewTour;
            }
        }

        public Tour CreateTour(int compId, short numberQuest, short NumberInComp, string name)
        {
            Competition dbCompetition = _CHGKContainer.Competitions.SingleOrDefault(t => t.ID == compId);
            Tour NewTour = new Tour();
            
            if (dbCompetition == null || NewTour == null)
            {
                return null;
            }
            else
            {
                NewTour.NumberOfQuestions = numberQuest;
                NewTour.CompetitionID = compId;
                NewTour.Name = name;
                NewTour.NumberInCompetition = NumberInComp;
                
                _CHGKContainer.Tours.AddObject(NewTour);
                _CHGKContainer.SaveChanges();
                
                return NewTour;
            }
        }

        public void UpdateTour(Tour userTour)
        {
            Tour dbTour = GetTour(userTour.ID);
            
            dbTour.NumberOfQuestions = userTour.NumberOfQuestions;
            dbTour.CompetitionID = userTour.CompetitionID;
            dbTour.Name = userTour.Name;
           
            _CHGKContainer.SaveChanges();
           
        }

        public void DeleteTour(int id)
        {
            Tour dbTour = GetTour(id);
           
            _CHGKContainer.Tours.DeleteObject(dbTour);
            _CHGKContainer.SaveChanges();

        }
    }
}